import {addGuPiao, deleteUserGuPiao, getUserAll, updateUserGuPiao} from "../api/gupiao/api.js";
let USERID;

// guPiaoAll {
//   id: 100001,
//   name: '123',
//   buyPrice: 4.5,
//   sellHigh: 5,
//   sellLow: 4,
//   isBuy: false,
//   price:4.3,
// }
/**
 * 获取监控的所有股票
 * @param guPiaoAll
 * @returns {Promise<*>}
 */
export const useGuPiao = async (guPiaoAll) => {
  USERID = localStorage['id'] || null;
  let data = await getUserAll(USERID);
  guPiaoAll.value = data.rows.map((item) => {
    return {
      id: item.gupiao_id,
      name: item.gupiao_name || '暂未实现',
      buyPrice: item.price_buy,
      sellHigh: item.price_sell_high,
      sellLow: item.price_sell_low,
      isBuy: !!item.do_it,
      price: item.gupiao_price || '暂未实现'
    }
  });
  // console.log(guPiaoAll.value)
  return guPiaoAll;
}

/**
 * 更新股票的买入状态
 * @param gupiao_id
 * @param result
 * @returns {Promise<void>}
 */
export const useUpdateGuPiaoBuy = async (gupiao_id, result) => {
  await updateUserGuPiao(USERID,gupiao_id,null,null,null,result?1:0);
}

/**
 * 删除股票 然后刷新
 * @param gupiao_id
 * @returns {Promise<void>}
 */
export const useDeleteGuPiao = async (gupiao_id) => {
  // 删除股票监控
  await deleteUserGuPiao(USERID, gupiao_id);
}

/**
 * 更新股票数据
 * @param gupiao_id
 * @param buyPrice
 * @param sellHigh
 * @param sellLow
 * @returns {Promise<void>}
 */
export const useUpdateGuPiao = async (gupiao_id, buyPrice, sellHigh, sellLow) => {
  await updateUserGuPiao(USERID, gupiao_id, buyPrice, sellHigh, sellLow);
}

export const useSaveGuPiao = async (gupiao_id, buyPrice, sellHigh, sellLow) => {
  let res = await addGuPiao(USERID, gupiao_id, buyPrice, sellHigh, sellLow);
  console.log(res);
}

